
listdir=$1

dir_now=`pwd`

for dir in `cat $listdir`
do
	cd $dir
	ls G*.fit | grep -v "Flat" | grep -v "Zero" >list
	Flatcom=`ls *.fit | grep "Flat"`
	for fitfile in `cat list`
	do
                if [  -r Zero.fit ] && [ -r $Flatcom   ]
                then
		Flatcom1=`echo $Flatcom | awk '{print($1"[1]")}'`
		echo "have zero and Flat images"
		xzerocorr.sh $fitfile Zero.fit
		echo "to correct the flat"
		xflatcorr.sh $fitfile $Flatcom1
		#imhead $fitfile
                #xzeroflatcorr.sh  $fitfile $Flatcom
                fi

                if [  -r Zero.fit ] && [ ! -r $Flatcom   ]
                then
		echo "have Zero but not Flat_R.fit"
                xzerocorr.sh  $fitfile Zero.fit
                fi

                if [  ! -r Zero.fit ]
                then
                        echo "No Zero.fit for $dir_now"
                fi


	done
	exit 0
done
echo "data process is over"
